package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class MultiImagesPanel extends JPanel {
	private ArrayList<BufferedImage> imagesList;
//	private int width;
//	private int height;

    public MultiImagesPanel(ArrayList<BufferedImage> l, int width, int height)
    {
        this.imagesList = l;
        this.setPreferredSize(new Dimension(width, height));
//        this.width = width;
//        this.height = height;
        this.setBackground(Color.black);
    }

    public void paintComponent(Graphics g)
    {
    	if (imagesList == null) {
    		return;
    	}
    	super.paintComponent(g);
    	
        Graphics g2 = g.create();
        
        g2.setColor(Color.black);
        
        Iterator<BufferedImage> it = imagesList.iterator();
        BufferedImage image = null;
        try {
            BufferedReader in = new BufferedReader(new FileReader("src/coord.txt"));
            String str = in.readLine();

			int maxX = 100;
			int maxY = 100;
			int counter = 0;
            while (it.hasNext()) {
    			image = (BufferedImage)it.next();
    			str = in.readLine();
    			String[] coords = str.split(" ");
    			int x = -100;
    			int y = -100;
    			for (String s:coords) {
    				if (!s.isEmpty() && x == -100) {
    					x = (int) ((Float.parseFloat(s))*400)+520;
    					if (x > maxX) {
    						maxX = x;
    					}
    				}
    				else if (!s.isEmpty()) {
    					y = (int) ((Float.parseFloat(s))*200)+250;
    					if (y > maxY) {
    						maxY = y;
    					}
    				}
    			}

            	if (image.getHeight() < image.getWidth()) {
        	        g2.drawImage(image, x, y, 100, (image.getHeight()*100)/image.getWidth(), null);
            	}
            	else {
            		g2.drawImage(image, x*counter, y*counter, (image.getWidth()*100)/image.getHeight(), 100, null);
            	}
            	counter++;
            }
            this.setPreferredSize(new Dimension(maxX, maxY));
            this.validate();
            in.close();
            g2.dispose();
            
        } catch (IOException e) {
        }
        
    }
}